/*
 * Copyright 2003-2005 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

using System;

namespace argot
{
	
	/// <summary> 
	/// An output stream class which allows Objects to be written to the 
	/// stream using the type system.
	/// </summary>
	public class TypeOutputStream
	{
		private TypeMap _map;
		private System.IO.Stream _out;
		
		public TypeOutputStream(System.IO.Stream outStream, TypeMap map)
		{
			_out = outStream;
			_map = map;
		}

		virtual public TypeMap TypeMap
		{
			get
			{
				return _map;
			}
			
		}


		public System.IO.Stream Stream
		{
			get
			{
				return _out;
			}
		}		
		
		public virtual void  WriteObject(int id, System.Object o)
		{
			TypeWriter writer = _map.GetWriter(id);
			TypeElement element = _map.GetStructure(id);
			writer.Write(this, o, element);
		}
		
		public virtual void  WriteObject(System.String name, System.Object o)
		{
			int id = this.TypeMap.GetId(name);
			if (id == TypeLibrary.NOTYPE)
				throw new TypeException("not found: " + name);
			WriteObject(id, o);
		}
		
	}
}